import glob, os

files_paths = glob.glob("/home/hnu/Downloads/v2x/v2x_yolo/labels/*.txt")
# files_paths += glob.glob("/disk1/yolodata/labels/test2/labels/*.txt")

# classesDict = {'0':'0', '1':'4', '2':'1', '3':'4', '4':'2', '5':'3', '6':'4', '7':'3'}
classesDict = {'0':'1', '1':'3', '2':'1', '3':'2', '4':'0', '5':'4', '6':'4', '7':'4', '8':'4'}

H, W = 1080, 1920
for filename in files_paths:
    with open(filename, 'r') as fp:
        linedata = fp.readlines()

    sa, sb = os.sep + 'labels' + os.sep, os.sep + 'yolo_labels' + os.sep
    save_path = sb.join(filename.rsplit(sa, 1))
    print(save_path)
    with open(save_path, 'w') as fp:
        for line in linedata:
            data = line.strip().split()
            if (data[0] == '9'):
                continue
            classes, [x1, y1, x2, y2] = classesDict[data[0]], data[1:5]

            x, y, w, h = (float(x1) + float(x2)) / 2 / W, (float(y1) + float(y2)) / 2 / H, (float(x2) - float(x1)) / W, (float(y2) - float(y1)) / H
            data_str = " ".join(str(i) for i in [classes, x, y, w, h]) + "\n"
            # print(data_str)
            fp.write(data_str)
